var Timer={
	setTimer:function(callbacker,param,interval){
		var newId=this.getNewId();
		this.timers[newId]=
			{
				callbacker:callbacker,
				param:param
			};

		setTimeout("Timer.handleTimeout("+newId+")",interval);
	},
	timers:[],
	getNewId:function(){
		var i=0;
		for(;i<this.timers.length;i++)
		{
			if(this.timers[i]==null)return i;
		}
		return i;
	},
	handleTimeout:function(id){
		var callbacker=this.timers[id].callbacker;
		var param=this.timers[id].param;
		this.timers[id]=null;
		callbacker.onTimeout(param);
		return;
	}
};

var Shell={
	obj:new ActiveXObject("WScript.Shell"),
	exec:function(cmd){
		return this.obj.exec(cmd);
	},
	run:function(cmd){
		return this.obj.Run(cmd,0,true);
	},
	runHiddenCommand:function(cmd){
		
		var outPath=File.getTempName();
		var errPath=File.getTempName();
		
		var fullCmd='%ComSpec% /c '+cmd+' 2>'+errPath+' 1>'+outPath;
		var ret={};
		ret.returnValue=this.run(fullCmd);
		ret.out=File.readAll(outPath);
		//alert(ret.out);
		ret.err=File.readAll(errPath);
		//alert(ret.err);
		return ret;
	}
}
var File={
	objFSO:new ActiveXObject("Scripting.FileSystemObject"),
	readAll:function(fn)
	{
		var oTextStream = this.objFSO.OpenTextFile(fn,1);
		if(oTextStream.AtEndOfStream)return '';
		var sFileContents = oTextStream.ReadAll();
		oTextStream.Close();
		return sFileContents;
	},
	getWindowsTemp:function()
	{
		return this.objFSO.GetSpecialFolder(2);
	},
	getTempName:function()
	{
		return this.objFSO.GetTempName();
	},
	getFullTempName:function()
	{
		return this.getWindowsTemp()+'\\'+this.getTempName();
	},
	convertUrlPathToLocalFilePath:function(url){
		return url.replace('file:///','').replace(/\//g,'\\');
	},
	makeSurePathExisted:function(folder,createIfNotExisted){
		
		var pathItems=folder.split('\\');
		currentCheckingFolder=pathItems[0];
		for(var i=1;i<pathItems.length;++i){
			currentCheckingFolder+='\\'+pathItems[i];
			//alert(currentCheckingFolder);
			if(File.objFSO.FolderExists(currentCheckingFolder))continue;
			if(!createIfNotExisted)return false;
			//alert('going to create folder:'+currentCheckingFolder);
			File.objFSO.CreateFolder(currentCheckingFolder);
		}
		return true;
	}
}

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = new obj.constructor(); // changed (twice)
    for(var key in obj)
        temp[key] = clone(obj[key]);

    return temp;
}

var AsyncProcessHelper=
{
	exec:function(requiredScriptFiles,contentScript,inputParamForContentScript,isDebug){
		
		var createWindowParam="unadorned:yes; dialogHeight:0px; dialogWidth:0px;";
		
		if(isDebug)
			var createWindowParam='';
			
		var childWindow=window.showModelessDialog('about:blank',inputParamForContentScript, createWindowParam); 
		var doc=childWindow.document;
		childWindow.returnValue=inputParamForContentScript;
		
		var toWriteContent=[];
		
		for(var i=0;i<requiredScriptFiles.length;i++){
			toWriteContent.push('<script src="'+requiredScriptFiles[i]+'"></script>');
		}
		
		toWriteContent.push('<div id="source">src</div>');
		toWriteContent.push('<script> function fx(){document.getElementById("source").innerText=document.body.innerHTML;('+contentScript+')(window.returnValue);window.close();} window.setTimeout("fx()",1);</script>');
		
		
		toWriteContent=toWriteContent.join('\n')
		doc.write(toWriteContent);
	}/*,
	execShellCommand:function(cmd,eventHandler){
		this.exec(['script/tools.js'],function(p){
			var fullCmd=p.cmd;
			var oExec=Shell.run(fullCmd,0,true);
		},
		{
			cmd:cmd,
			eventHandler:eventHandler
		}
	);}*/
};

function getDumpObjString(e,indent)
{
	var ret=[];
	for(var x in e){
		var line="";
		for(var i=0;i<indent;i++){
			line+='\t';
		}
		line+=x+':'+e[x];
		ret.push(line);
		switch(typeof(e[x])){
		case 'object':
			ret.concat(getDumpObjString(e[x],indent+1));
		}
	}
	return ret;
}

function urlPathToLocalFilePath(url){
	url=''+url;
	if(url.indexOf('file:///')==0)
		url=url.substring(8);
		
	url=url.replace(/\//g,'\\');
	return url;
}